package com.ffcs.web.services;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.ffcs.web.daos.implementations.MessageDao;
import com.ffcs.web.daos.implementations.TucaoAnswerDao;
import com.ffcs.web.model.entity.Message;
import com.ffcs.web.model.entity.SystemMessage;
import com.ffcs.web.model.entity.TucaoAnswer;

@Service
public class MessageService {

	@Autowired
	private MessageDao messageDao;
	@Autowired
	private TucaoAnswerDao tucaoAnswerDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<Message> getMessage(String sendType) {
		List<Message> messageList=messageDao.getMessage(sendType);
		for (Message message : messageList) {
			message.setFlag("0");
		}
		return messageList;
	}
	public List<TucaoAnswer> getTucaoAnswer(Integer userId) {
		List<TucaoAnswer> tucaoAnswerList=tucaoAnswerDao.findByPropertyNameWithOrder("userId", userId, "answerDate", false);
		return tucaoAnswerList;
	}
	public String getTucao(Integer tucaoId) {
		String sql="select tucaoContent from wechat_tucao where id="+tucaoId;
		return jdbcTemplate.queryForObject(sql,String.class);
	}
	public boolean hasMes(Integer id,String type) {
		String sql="select MAX(i.newTime) from wechat_isMessage i where i.user_id="+id+" and i.type="+type;
		Date date=jdbcTemplate.queryForObject(sql,Date.class);
		System.out.println(sql);
		StringBuilder sql2=new StringBuilder("select MAX(da.d) from (");
		sql2.append(" select MAX(answerDate) as d from wechat_tucao_answer where userId="+id+" ");
		sql2.append("  UNION ");
		sql2.append(" select MAX(createDate) as d from wechat_system_message where userId="+id+" and type="+type);
		sql2.append(" UNION ");
		sql2.append(" select MAX(createDate) as d from wechat_message where sendType=0 or sendType="+type);
		sql2.append(" ) as da ");
		System.out.println(sql2);
		Date date2=jdbcTemplate.queryForObject(sql2.toString(),Date.class);
		
		if(null==date && null!=date2){
			return true;
		}
		
		if(null==date2 && null!=date){
			return false;
		}
		
		if(null==date2 && null==date){
			return false;
		}
		
		if(date.getTime()<date2.getTime()){
			return true;
		}else{
			return false;
		}
	}
	public void removeRed(Integer id,String type) {
		
		String sql="select count(*) from wechat_isMessage where user_id="+id+" and type="+type;
		int count=jdbcTemplate.queryForObject(sql,Integer.class);
		if(count>0){
			sql="update wechat_isMessage set newTime=now() where user_id="+id+" and type="+type;
		}else{
			sql="insert into wechat_isMessage values("+id+",now(),"+type+")";
		}
		System.out.println("更新消息红点表:"+sql);
		jdbcTemplate.update(sql);
	}
	
	/**
	 * 更新消息红点表
	 * @param id
	 * @param type
	 */
	public void inserRed2(Integer id,String type){
		String sql="update wechat_isMessage set newTime=now() where user_id="+id+" and type="+type;
		System.out.println("更新消息红点表:"+sql);
		jdbcTemplate.update(sql);
	}
	
	public void inserSystemMessage(Integer userId,String type,String content){
		String sql="insert wechat_system_message(createDate,content,type,userId) values (now(),'"+content+"','"+type+"','"+userId+"')";
		System.out.println(sql);
		jdbcTemplate.update(sql);
	}
	
	public List<SystemMessage> getSysMessageList(Integer id, String sendType) {
		String sql="select * from wechat_system_message where userId="+id+" and type="+sendType+" order by createDate desc";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<SystemMessage>(SystemMessage.class));
	}
}
